package com.mvp.lt.example.websocket;

import android.util.SparseArray;

import org.java_websocket.client.WebSocketClient;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.Scanner;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

/**
 * Created by ${LiuTao}.
 * User: Administrator
 * Name: AirLineView
 * functiona:
 * Date: 2019/6/19 0019
 * Time: 下午 18:45
 */
public class Test {

    public static void main(String[] args) throws URISyntaxException {
        // ws://192.168.8.60:1234
        // wss://www.redovation.com:81
        // "ws://134.175.247.171:99"
        // ws://134.175.247.171:99/
//        SocketClient(new URI("wss://yhsw.hnlinkeda.com:98"));
        // WS_URL = "ws://" + serverIp + ":" + Constant.SERVER_SOCKET_POINT_VALUE + "/";
        WebSocketClient client = new SocketClient(new URI("ws://192.168.1.120:8080/websocket"));
        // wss需添加
//        SSLContext sslContext = null;
//        try {
//            sslContext = SSLContext.getInstance("TLS");
//            sslContext.init(null, new TrustManager[]{new X509TrustManager() {
//                @Override
//                public void checkClientTrusted(X509Certificate[] chain,
//                                               String authType) {
//                }
//
//                @Override
//                public void checkServerTrusted(X509Certificate[] chain,
//                                               String authType) {
//                }
//                @Override
//                public X509Certificate[] getAcceptedIssuers() {
//                    return new X509Certificate[0];
//                }
//            }}, new SecureRandom());
//            SSLSocketFactory factory = sslContext.getSocketFactory();
//            client.setSocketFactory(factory);
        client.connect();
//        } catch (NoSuchAlgorithmException e) {
//            e.printStackTrace();
//        }catch (KeyManagementException e) {
//            e.printStackTrace();
//        }
        System.out.println("请输入");
        while (true) {
            Scanner input = new Scanner(System.in);
            String result = input.next();
            client.send(result);
        }
    }

    //
//    public static void main(String[] args) throws Exception {
//        SparseArray<String> sparseArray = new SparseArray();
//        sparseArray.put(1,"wwwww");
//        sparseArray.put(2,"wwwww");
//        sparseArray.put(3,"wwwww");
//        sparseArray.put(4,"wwwww");
//////    建立连接
////        Socket sSocket = new Socket("134.175.130.23", 8890);
////        new Thread(new DD()).start();
//////先开启接收端的线程
////        new Thread(new TT()).start();
//////在开启发送端的线程
////
////        HashMap<Integer, String> list = new HashMap<>();
////
////        list.put(1, "jjjjjjjjjjjjjjjjj");
////        list.put(4, "PPPPPPPPPPPPPPPP");
////
////        for (Map.Entry<Integer, String> entry : list.entrySet()) {
////            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
////        }
////
////
////        List<String> strList = new ArrayList<String>(list.values());
////
////        for (int i = 0; i < strList.size(); i++) {
////            System.out.println(i + "---" + strList.get(i));
////        }
////        int[] ints = {118, 101, 105, 127, 112};
////
////        bubbleSort(ints);
////        double gpsInfo = 28.125456;
////        int d = (int) gpsInfo;//115
////        int m = (int) ((gpsInfo - d) * 60);//0.218803*60 =  13.12818
////        double s = ((gpsInfo - d) * 60 - m) * 60;
//
//
////        System.out.println(d + "----" + m + "---" + s);
//        double degrees = 60.0;
//        double radians = 0.3333;
////        double radians = Math.toRadians(degrees);
//
////        System.out.format("pi 的值为 %.4f%n", Math.PI);
////        System.out.format("弧度 %.4f%n", radians);
////        System.out.format("%.4f 的反余弦值为 %.0f 度 %n", Math.cos(radians), Math.toDegrees(Math.acos(0.3333)));
////
////        getTrangleAngle(0, 1, 1, 2, 2, 1);
//    }

    public static void getTrangleAngle(double x1, double y1, double x2, double y2, double x3, double y3) {
//         Math.acos()

        double c = Math.abs(Math.sqrt(Math.pow((x1 - x2), 2) + Math.sqrt(Math.pow((y1 - y2), 2))));

        double a = Math.abs(Math.sqrt(Math.pow((x3 - x2), 2) + Math.sqrt(Math.pow((y3 - y2), 2))));

        double b = Math.abs(Math.sqrt(Math.pow((x1 - x3), 2) + Math.sqrt(Math.pow((y1 - y3), 2))));

        double cosb = (Math.pow(a, 2) + Math.pow(c, 2) - Math.pow(b, 2)) / (2 * a * c);
        System.out.format(" %.4f%n", cosb);
        double angle = Math.toDegrees(Math.acos(cosb));
        System.out.format(" 角度%.4f%n", angle);
        cacleHypotenuseLong(angle / 2);
    }


    public static void cacleHypotenuseLong(double angle) {
        System.out.format(" ---angle--- %.4f%n", angle);
        System.out.format(" ------ %.4f%n", Math.sin(Math.toRadians(angle)));
        double hlong = Math.abs(1 / Math.sin(Math.toRadians(angle)));
        System.out.format(" 斜边长%.4f%n", hlong);
    }

    //冒泡排序算法
    static void bubbleSort(int[] data) {
        int temp;

        for (int i = 1; i < data.length; i++) {
            for (int j = 0; j < data.length - i; j++) {
                if (data[j] > data[j + 1]) {
                    temp = data[j + 1];
                    data[j + 1] = data[j];
                    data[j] = temp;
                }
            }
            System.out.print(i + "次排序:");
            for (int j = 0; j < data.length; j++) {
                System.out.print(" " + data[j]);
            }
            System.out.println("\n");
        }

    }

    //接收端的代码
    public static class DD implements Runnable {

        @Override
        public void run() {
            try {
                ServerSocket ss = new ServerSocket(8890);
//创建一个serversocket其端口与发送端的端口是一样的
                Socket s = ss.accept();
//侦听并接受到此套接字的连接，返回一个socket对象
                InputStream is = s.getInputStream();
//获取到输入流
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                byte[] buf = new byte[1024];
//接收收到的数据
                int line = 0;
                while ((line = is.read(buf)) != -1) {
                    System.out.println("-----客户端输入的信息是-----");
                    System.out.println(new String(buf, 0, line));
//将接收到的数据在控制台输出
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }
    }

    //8888
    //发送端的代码如下：
    public static class TT implements Runnable {

        @Override
        public void run() {
            try {
                Socket s = new Socket("134.175.130.23", 8890);
//创建一个socket绑定的端口和地址为：9977，本机。
                OutputStream oos = s.getOutputStream();
//获取到输出流 System.out.println("-----客户端输入的信息是-----");
                System.out.println("-----请输入-----");
                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(oos));
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                String line = null;
                while ((line = br.readLine()) != null) {
                    bw.write(line);
                    bw.flush();
//将内容写到控制台
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }

    }
}
